/*
 *
 *
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.osdtsystem.matrixparser.featureextraction;

import org.osdtsystem.matrixparser.data.CONLLSentence;
import org.osdtsystem.matrixparser.features.Feature;
import org.osdtsystem.matrixparser.features.FeatureHandler;
import org.osdtsystem.matrixparser.features.FeatureVector;
import org.osdtsystem.matrixparser.features.SparseFeatureVector;

/**
 *
 * @author mwh.isv
 */
public class MST2OFeatureExtractorTemporary {

    public static FeatureVector get2OFeatures(CONLLSentence instance, int head, int dep, int prevChild, FeatureHandler fh) {
        FeatureVector fv = new SparseFeatureVector();

        addTripFeatures(instance, head, prevChild, dep, fv, fh);
        addSiblingFeatures(instance, prevChild, dep, prevChild==head, fv, fh);

        return fv;

    }

    private static void addSiblingFeatures(CONLLSentence instance,
            int ch1, int ch2,
            boolean isST,
            FeatureVector fv,
            FeatureHandler fh) {

        // ch1 is always the closes to par
        String dir = ch1 > ch2 ? "RA" : "LA";

        String ch1_pos = isST ? "STPOS" : instance.get(ch1).postag();
        String ch2_pos = instance.get(ch2).postag();
        String ch1_word = isST ? "STWRD" : instance.get(ch1).form();
        String ch2_word = instance.get(ch1).form();

        Feature f;

        f = fh.getFeature("CH_PAIR=" + ch1_pos + "_" + ch2_pos + "_" + dir);
        fv.addValue(f, 1.0);

        f = fh.getFeature("CH_WPAIR=" + ch1_word + "_" + ch2_word + "_" + dir);
        fv.addValue(f, 1.0);

        f = fh.getFeature("CH_WPAIRA=" + ch1_word + "_" + ch2_pos + "_" + dir);
        fv.addValue(f, 1.0);

        f = fh.getFeature("CH_WPAIRB=" + ch1_pos + "_" + ch2_word + "_" + dir);
        fv.addValue(f, 1.0);

        f = fh.getFeature("ACH_PAIR=" + ch1_pos + "_" + ch2_pos);
        fv.addValue(f, 1.0);

        f = fh.getFeature("ACH_WPAIR=" + ch1_word + "_" + ch2_word);
        fv.addValue(f, 1.0);

        f = fh.getFeature("ACH_WPAIRA=" + ch1_word + "_" + ch2_pos);
        fv.addValue(f, 1.0);

        f = fh.getFeature("ACH_WPAIRB=" + ch1_pos + "_" + ch2_word);
        fv.addValue(f, 1.0);

        int dist = Math.max(ch1, ch2) - Math.min(ch1, ch2);
        String distBool = "0";
        if (dist > 1) {
            distBool = "1";
        }
        if (dist > 2) {
            distBool = "2";
        }
        if (dist > 3) {
            distBool = "3";
        }
        if (dist > 4) {
            distBool = "4";
        }
        if (dist > 5) {
            distBool = "5";
        }
        if (dist > 10) {
            distBool = "10";
        }

        f = fh.getFeature("SIB_PAIR_DIST=" + distBool + "_" + dir);
        fv.addValue(f, 1.0);

        f = fh.getFeature("ASIB_PAIR_DIST=" + distBool);
        fv.addValue(f, 1.0);

        f = fh.getFeature("CH_PAIR_DIST=" + ch1_pos + "_" + ch2_pos + "_" + distBool + "_" + dir);
        fv.addValue(f, 1.0);

        f = fh.getFeature("ACH_PAIR_DIST=" + ch1_pos + "_" + ch2_pos + "_" + distBool);
        fv.addValue(f, 1.0);


    }

    private static void addTripFeatures(CONLLSentence instance,
            int par,
            int ch1, int ch2,
            FeatureVector fv,
            FeatureHandler fh) {



        // ch1 is always the closest to par
        String dir = par > ch2 ? "RA" : "LA";

        String par_pos = instance.get(par).postag();

        String ch1_pos = ch1 == par ? "STPOS" : instance.get(ch1).postag();
        String ch2_pos = instance.get(ch2).postag();

        String pTrip = par_pos + "_" + ch1_pos + "_" + ch2_pos;

        Feature f = fh.getFeature("POS_TRIP=" + pTrip + "_" + dir);
        fv.addValue(f, 1.0);
        f = fh.getFeature("APOS_TRIP=" + pTrip);
        fv.addValue(f, 1.0);
    }
}
